package com.yc.projectDemo;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class NHCScraper {

	public static void main(String[] args) {
		String url = "http://www.nhc.gov.cn/wjw/xwdt/list.shtml";

		try {
			// 连接并获取 HTML 文档
			Document doc = Jsoup.connect(url)
				.userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36")
				.referrer("http://www.nhc.gov.cn/")  // 伪装来源
				.header("Origin", "http://www.nhc.gov.cn")
				.header("Cookie", "sVoELocvxVW0S=5u3VAGkQSkHvi.zC_G6OECpd0v7VX5vO5Wp3JU_N3vRj4Sk9TeDMY1SCUgUVT9XxGEei9fKJWmZXgpUkkFQa49G; insert_cookie=91349450; arialoadData=true; ariawapChangeViewPort=false; _yfxkpy_ssid_10006654=%7B%22_yfxkpy_firsttime%22%3A%221743144200899%22%2C%22_yfxkpy_lasttime%22%3A%221743144200899%22%2C%22_yfxkpy_visittime%22%3A%221743146098859%22%2C%22_yfxkpy_cookie%22%3A%2220250328144320900790971444266984%22%7D; ariauseGraymode=false; sVoELocvxVW0T=5R5RW9CIbsoEqqqDYCygX0av4yNabyIWErnZSWmkXhPxh_cW5of4HSoxlke_nS7i9PMA3FJ3gxmOhhrviX075EMnwhcHtqaJ0txXKaDGcWNKYAdtz.Fp.bruLs9_3m_YoPHZEnIvfuDWvDjdcqK7nn2WGV2KMHdydrGAwQ7VaCUUsnXdzV4SblJRJreEVSDqfRg.u1PIa.xfwsJGR6dJz54rT7KTk4LbxyYY_tJ0CIRudlifoxmEylvzzFe4a9l1vDu1suMtjwfU6wNy_PZQsrfzShh55OXfS9uQrxrqrMJ9mFjs0c9RN034aieDp4xybm0_cUJR80s7ohSSliwRHhe")
				.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
				.header("Accept-Language", "zh-CN,zh;q=0.9")
				.header("Connection", "keep-alive")
				.header("Proxy-Connection", "keep-alive")
				.timeout(5000)  // 设置超时时间
				.get();

			// 解析新闻列表
			Elements newsItems = doc.select(".list li");

			for (Element item : newsItems) {
				// 获取标题
				Element link = item.selectFirst("a");
				String title = link.text();
				String newsUrl = "http://www.nhc.gov.cn" + link.attr("href");

				// 获取时间
				String date = item.selectFirst("span").text();

				// 输出
				System.out.println("标题: " + title);
				System.out.println("链接: " + newsUrl);
				System.out.println("日期: " + date);
				System.out.println("---------------------------");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
